<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库ID" prop="storageId">
        <el-input
          v-model="queryParams.storageId"
          placeholder="请输入仓库ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
    
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['user:medication:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:medication:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:medication:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:medication:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="medicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="仓库ID" align="center" prop="storageId" />
      <el-table-column label="时间" align="center" prop="oprTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.oprTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="老人ID" align="center" prop="personId" />
      <el-table-column label="1入库，2领用" align="center" prop="orderType" />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['user:medication:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:medication:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用药记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="storageId">
          <el-input v-model="form.storageId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="操作员" prop="adminId">
          <el-input v-model="form.adminId" placeholder="请输入操作员" />
        </el-form-item>
        <el-form-item label="时间" prop="oprTime">
          <el-date-picker clearable
            v-model="form.oprTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注/医嘱" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="老人ID" prop="personId">
          <el-input v-model="form.personId" placeholder="请输入老人ID" />
        </el-form-item>
        <el-form-item label="出入库" prop="orderType">
          <el-input v-model="form.orderType" placeholder="1入库，2领用" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <!-- <el-divider content-position="center">老人信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPersonInfo">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePersonInfo">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="personInfoList" :row-class-name="rowPersonInfoIndex" @selection-change="handlePersonInfoSelectionChange" ref="personInfo">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="$comment" prop="communityId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.communityId" placeholder="请输入$comment" />
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="userName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userName" placeholder="请输入姓名" />
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="mobile" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mobile" placeholder="请输入手机号" />
            </template>
          </el-table-column>
          <el-table-column label="密码" prop="password" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.password" placeholder="请输入密码" />
            </template>
          </el-table-column>
          <el-table-column label="性别(男/女)" prop="sex" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.sex" placeholder="请选择性别(男/女)">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="年龄" prop="age" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.age" placeholder="请输入年龄" />
            </template>
          </el-table-column>
          <el-table-column label="身份证号" prop="cardNo" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cardNo" placeholder="请输入身份证号" />
            </template>
          </el-table-column>
          <el-table-column label="医保卡号" prop="medicalCard" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.medicalCard" placeholder="请输入医保卡号" />
            </template>
          </el-table-column>
          <el-table-column label="紧急联系人" prop="emergentContact" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.emergentContact" placeholder="请输入紧急联系人" />
            </template>
          </el-table-column>
          <el-table-column label="紧急联系人电话" prop="contactMobile" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.contactMobile" placeholder="请输入紧急联系人电话" />
            </template>
          </el-table-column>
          <el-table-column label="类别(1：未验证，2：已验证)" prop="state" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.state" placeholder="请输入类别(1：未验证，2：已验证)" />
            </template>
          </el-table-column>
          <el-table-column label="身份证图片正面" prop="cardPic1" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cardPic1" placeholder="请输入身份证图片正面" />
            </template>
          </el-table-column>
          <el-table-column label="身份证图片背面" prop="cardPic2" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cardPic2" placeholder="请输入身份证图片背面" />
            </template>
          </el-table-column>
          <el-table-column label="家庭地址" prop="addr" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.addr" placeholder="请输入家庭地址" />
            </template>
          </el-table-column>
          <el-table-column label="微信openid" prop="openId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.openId" placeholder="请输入微信openid" />
            </template>
          </el-table-column>
          <el-table-column label="是否入住 0-未入住 1-已入住" prop="useful" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.useful" placeholder="请输入是否入住 0-未入住 1-已入住" />
            </template>
          </el-table-column>
        </el-table> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMedication, getMedication, delMedication, addMedication, updateMedication } from "@/api/user/medication";

export default {
  name: "Medication",
  data() {
    return {
      // 状态数据字典
      stateOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPersonInfo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用药记录表格数据
      medicationList: [],
      // 老人信息表格数据
      personInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        storageId: null,
        orderType: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    // 状态字典
    // this.getDict("order_type").then(data => {
    //   this.stateOptions = data; 
    // })
  },
  methods: {
    /** 查询用药记录列表 */
    getList() {
      this.loading = true;
      listMedication(this.queryParams).then(response => {
        this.medicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNo: null,
        storageId: null,
        adminId: null,
        oprTime: null,
        remark: null,
        personId: null,
        orderType: null,
        state: null,
        createTime: null
      };
      this.personInfoList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用药记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getMedication(orderId).then(response => {
        this.form = response.data;
        this.personInfoList = response.data.personInfoList;
        this.open = true;
        this.title = "修改用药记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.personInfoList = this.personInfoList;
          if (this.form.orderId != null) {
            updateMedication(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMedication(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除用药记录编号为"' + orderIds + '"的数据项？').then(function() {
        return delMedication(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 老人信息序号 */
    rowPersonInfoIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 老人信息添加按钮操作 */
    handleAddPersonInfo() {
      let obj = {};
      obj.communityId = "";
      obj.userName = "";
      obj.mobile = "";
      obj.password = "";
      obj.sex = "";
      obj.age = "";
      obj.cardNo = "";
      obj.medicalCard = "";
      obj.emergentContact = "";
      obj.contactMobile = "";
      obj.faceUrl = "";
      obj.state = "";
      obj.cardPic1 = "";
      obj.cardPic2 = "";
      obj.addr = "";
      obj.openId = "";
      obj.remark = "";
      obj.useful = "";
      this.personInfoList.push(obj);
    },
    /** 老人信息删除按钮操作 */
    handleDeletePersonInfo() {
      if (this.checkedPersonInfo.length == 0) {
        this.$modal.msgError("请先选择要删除的老人信息数据");
      } else {
        const personInfoList = this.personInfoList;
        const checkedPersonInfo = this.checkedPersonInfo;
        this.personInfoList = personInfoList.filter(function(item) {
          return checkedPersonInfo.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePersonInfoSelectionChange(selection) {
      this.checkedPersonInfo = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/medication/export', {
        ...this.queryParams
      }, `medication_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
